package com.college_career_system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.college_career_system.dao.ProfessionMapper;
import com.college_career_system.pojo.po.*;
import com.college_career_system.dao.SchoolMapper;
import com.college_career_system.service.SchoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college_career_system.utils.ResponseCode;
import com.college_career_system.utils.ResponseResult;
import com.college_career_system.utils.StringUtil;
import com.college_career_system.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Empty
 * @since 2022-12-06
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private ProfessionMapper professionMapper;

    @Override
    public JSONObject selectUserEnrollment(HttpServletRequest request, String profession, String year, int flag) {

        System.out.println(TokenUtil.getId(request));
        QueryWrapper<School> wrapper = new QueryWrapper<>();
        wrapper.eq("school_id", TokenUtil.getId(request));
        System.out.println(wrapper == null);
        String school = schoolMapper.selectOne(wrapper).getSchoolName();
        System.out.println(schoolMapper.selectOne(wrapper));
        System.out.println(school);
        CountSalary allSalary = null;
        List<UserEnrollment> userEnrollments = new ArrayList<>();
        if(!StringUtil.isEmptyOrNull(profession)) {
            userEnrollments = schoolMapper.selectAllUserEnterpriseByProfession(school, profession, year, flag);
            allSalary = schoolMapper.getAllSalary2(school, profession, year);
        } else {
            userEnrollments = schoolMapper.selectAllUserEnterprise(school, year, flag);
            allSalary = schoolMapper.getAllSalary(school, year);
        }
        System.out.println(userEnrollments);
        System.out.println(allSalary);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userEnrollments", userEnrollments);
        jsonObject.put("allSalary", allSalary);
        return ResponseResult.success(jsonObject);
    }

    @Override
    public JSONObject selectAllYear(HttpServletRequest request) {

        QueryWrapper<School> wrapper = new QueryWrapper<>();
        wrapper.eq("school_id", TokenUtil.getId(request));
        return ResponseResult.success(schoolMapper.getAllYear(schoolMapper.selectOne(wrapper).getSchoolName()));
    }

    @Override
    public JSONObject selectAllProfession() {

        //QueryWrapper<Profession> wrapper = new QueryWrapper<>();
        return ResponseResult.success(professionMapper.selectList(new QueryWrapper()));
    }

    @Override
    public JSONObject updatePassWordById(HttpServletRequest request, String pwd) {
        if (StringUtil.isEmptyOrNull(pwd)){
            return ResponseResult.failure("传入密码为空", ResponseCode.STATUS_ERROR);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if (schoolMapper.updatePassWordById(TokenUtil.getId(request),bCryptPasswordEncoder.encode(pwd)) > 0){
            return ResponseResult.success("更新成功");
        }
        return ResponseResult.failure("更新失败",ResponseCode.STATUS_ERROR);
    }
}
